﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

public class XMLParser
{
    private string file;

    public XMLParser(string file)
    {
        this.file = file;
    }

    private IEnumerable<XElement> GenericExtract(string firstLevelName)
    {
        XElement doc = XElement.Load(file);

        var Query = from firstLevel in doc.Elements()
                    where (firstLevel.Attribute("name").Value == firstLevelName)
                    select firstLevel;
        return Query;
    }

    private IEnumerable<XElement> GenericExtract(string firstLevelName, string secondLevelName)
    {
        XElement doc = XElement.Load(file);

        var Query = from secondLevel in
                        (from firstLevel in doc.Elements()
                         where (firstLevel.Attribute("name").Value == firstLevelName)
                         select firstLevel).Elements()
                    where (secondLevel.Attribute("name").Value == secondLevelName)
                    select secondLevel;
        return Query;
    }

    private IEnumerable<XElement> GenericExtract(string firstLevelName, string secondLevelName, string thirdLevelName)
    {
        XElement doc = XElement.Load(file);

        var Query = from thirdLevel in
                        (
                            from secondLevel in
                                (from firstLevel in doc.Elements()
                                 where (firstLevel.Attribute("name").Value == firstLevelName)
                                 select firstLevel).Elements()
                            where (secondLevel.Attribute("name").Value == secondLevelName)
                            select secondLevel).Elements()
                    where (thirdLevel.Attribute("name").Value == thirdLevelName)
                    select thirdLevel;
        return Query;
    }

    public List<string> ExtractAttribute(string firstLevelName, string secondLevelName, string thirdLevelName, string attribute)
    {
        List<string> attributeList = new List<string>();
        IEnumerable<XElement> elements;
        if (String.IsNullOrEmpty(thirdLevelName))
            elements = GenericExtract(firstLevelName, secondLevelName);
        else
            elements = GenericExtract(firstLevelName, secondLevelName, thirdLevelName);

        foreach (XElement element in elements.Nodes())
        {
            attributeList.Add(element.Attribute(attribute).Value.ToString());
        }
        return attributeList;
    }

    public Dictionary<string, string> ExtractAttributePairs(string firstLevelName, string secondLevelName, string thirdLevelName, string attribute1, string attribute2)
    {
        Dictionary<string, string> attributePairs = new Dictionary<string, string>();
        IEnumerable<XElement> elements;

        if (String.IsNullOrEmpty(thirdLevelName))
            if (String.IsNullOrEmpty(secondLevelName))
                elements = GenericExtract(firstLevelName);
            else
                elements = GenericExtract(firstLevelName, secondLevelName);
        else
            elements = GenericExtract(firstLevelName, secondLevelName, thirdLevelName);

        foreach (XElement element in elements.Nodes())
        {
            if (element.Attribute(attribute2) == null)
            {
                attributePairs.Add(element.Attribute(attribute1).Value.ToString(), string.Empty);
            }
            else
            {
                attributePairs.Add(element.Attribute(attribute1).Value.ToString(), element.Attribute(attribute2).Value.ToString());
            }
        }
        return attributePairs;
    }

    public List<string> ExtractFirstLevel(string attribute)
    {
        List<string> fileslist = new List<string>();
        XElement doc = XElement.Load(file);
        foreach (XElement element in doc.Nodes())
        {
            fileslist.Add(element.Attribute(attribute).Value.ToString());
        }
        return fileslist;
    }


    public Dictionary<string, string> ExtractFirstAtributPairs(string attribute, string secondattribute)
    {
        Dictionary<string, string> fileslist = new Dictionary<string, string>();
        XElement doc = XElement.Load(file);
        foreach (XElement element in doc.Nodes())
        {
            fileslist.Add(element.Attribute(attribute).Value.ToString(), element.Attribute(secondattribute).Value.ToString());
        }
        return fileslist;
    }

    public List<string> ExtractSecondLevel(string firstLevelName, string secondLevelAttribute)
    {
        return XElement.Load(file).Elements().First(element => element.Attribute("name").Value.Equals(firstLevelName)).Elements().Select(element => element.Attribute(secondLevelAttribute).Value).ToList();
    }

    public List<string> ExtractThirdLevel(string firstLevelName, string secondLevelName, string thirdLevelAttribute)
    {
        return XElement.Load(file).Elements().First(element => element.Attribute("name").Value.Equals(firstLevelName)).Elements().First(element => element.Attribute("name").Value.Equals(secondLevelName)).Elements().Select(element => element.Attribute(thirdLevelAttribute).Value).ToList();
    }

}

